package com.wanmait.deliverymangementsystem.usercontroller;

import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Tasks;
import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.OrdersService;
import com.wanmait.deliverymangementsystem.service.TaskService;
import com.wanmait.deliverymangementsystem.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;

@Controller
@RequestMapping("user")
public class UserOrderController {
    @Resource
    private TaskService taskService;
    @Resource
    private UsersService usersService;
    @Resource
    private OrdersService ordersService;
    @GetMapping("waitAcceptOrderList")
    public PageInfo<Orders> waitAcceptOrderList(HttpSession session, HttpServletRequest request,Integer page){
        Users user =(Users) session.getAttribute("user");
        if (page==null)
            page=0;
        PageInfo<Orders> ordersPageInfo= ordersService.userWaitOrderFind(user.getId(),page);
        request.setAttribute("ordersPageInfo",ordersPageInfo);
        return ordersPageInfo;
    }


    @GetMapping("cancelBeforeAccept")
    public String cancelBeforeAccept(Integer id, HttpSession session){
        Orders orders= ordersService.findOrder(id);
        ordersService.cancelBeforeAccept(id);
        Tasks tasks= taskService.findByID(orders.getTask().getId());
        taskService.cancelTask(tasks.getId());
        Users user =(Users) session. getAttribute("user");
        double money=(user.getWallet()+tasks.getReward().doubleValue());
        DecimalFormat df =new DecimalFormat("#.00");
        money=Double.parseDouble(df.format(money));
        user.setWallet(money);
        usersService.charge(user);
        session.setAttribute("user",user);
        return "redirect:/user/waitAcceptOrderList";
    }
    //完成的订单
    @GetMapping("completedOrderList")
    @ModelAttribute("ordersPageInfo")
    public PageInfo<Orders> completedOrderList(@SessionAttribute Users user,Integer page){
        if (page==null)
            page=0;
       PageInfo<Orders> ordersPageInfo= ordersService.findUserCompleteOrderList(user.getId(), page);
        return ordersPageInfo;
    }

    //正在进行的任务
    @GetMapping("taskingList")
    @ModelAttribute("ordersPageInfo")
    public PageInfo<Orders> taskingList(@SessionAttribute Users user,Integer page){
        if (page==null)
        page=0;
        PageInfo<Orders> ordersPageInfo = ordersService.findUserTaskingOrderList(user.getId(), page);
        System.out.println(ordersPageInfo.getList());
        return ordersPageInfo;
    }

    @GetMapping("deleteOrder")
    public String deleteOrder(Integer id){
        ordersService.deleteUser(id);
        return "redirect:/user/completedOrderList";
    }

}
